﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace FlowCard
{
    public partial class frmSetSwdh : Form
    {
        SQLTools sqt;
        public string strSelectedSwdh = string.Empty;
        public string strSelectedNdjcwl = string.Empty;
        bool bSelectToClose = false;

        public frmSetSwdh()
        {
            InitializeComponent();
        }

        private void frmSetSwdh_Load(object sender, EventArgs e)
        {
            sqt = new SQLTools();
        }

        private void iniDatagridView()
        {
            string strSelectString = "select 丝网代号,内电极错位量 from Swdh order by 丝网代号";
            dgv1.DataSource = sqt.GetDataTable(strSelectString);
            dgv1.AutoResizeColumns();
        }

        private void dgv1_CurrentCellChanged(object sender, EventArgs e)
        {
            if (dgv1.CurrentRow != null)
            {
                txtSwdh.Text = GetString(dgv1.CurrentRow.Cells["c1"].Value);
                txtNdjcwl.Text = GetString(dgv1.CurrentRow.Cells["c2"].Value);
            }
        }

        /// <summary>
        /// 将对象转换为字符串
        /// </summary>
        /// <param name="tmpobj">要转换的对象（默认值为string.Empty）</param>
        /// <returns>字符串结果</returns>
        public static string GetString(object tmpobj)
        {
            try
            {
                return tmpobj.ToString();
            }
            catch
            {
                return string.Empty;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtSwdh.Text.Trim().Equals(string.Empty))
            {
                MessageBox.Show("丝网代号不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtSwdh.Focus();
                return;
            }
            if (txtNdjcwl.Text.Trim().Equals(string.Empty))
            {
                MessageBox.Show("内电极错位量不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtNdjcwl.Focus();
                return;
            }
            //检查丝网代号是否已存在
            if (sqt.RecordExist("select 丝网代号 from Swdh where 丝网代号='" + txtSwdh.Text.Trim() + "'"))
            {
                MessageBox.Show("丝网代号：" + txtSwdh.Text.Trim() + "已存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            //插入记录
            sqt.DoCmdExecuteNonQuery("insert into Swdh (丝网代号,内电极错位量) values ('" + txtSwdh.Text.Trim() + "','" + txtNdjcwl.Text.Trim() + "')");
            iniDatagridView();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgv1.CurrentRow != null)
            {
                if (MessageBox.Show("确定删除丝网代号：" + txtSwdh.Text.Trim() + "的记录吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    sqt.DoCmdExecuteNonQuery("delete from Swdh where 丝网代号='" + txtSwdh.Text.Trim() + "'");
                    iniDatagridView();
                }
            }
        }

        private void frmSetSwdh_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (bSelectToClose )
            {
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                this.DialogResult = DialogResult.Cancel;
            }
        }

        private void dgv1_DoubleClick(object sender, EventArgs e)
        {
            if (dgv1 .CurrentRow!=null)
            {
                strSelectedSwdh = GetString(dgv1.CurrentRow.Cells["c1"].Value);
                strSelectedNdjcwl= GetString(dgv1.CurrentRow.Cells["c2"].Value);
                bSelectToClose = true;
            }
        }
    }
}
